图像(Image)
表示可被 渲染器(Renderer) 绘制的图像资源。
type DrawImage = {
myImage: Image?,
sampler: ImageSampler?,
}
function init(self: DrawImage, context: Context): boolean
self.myImage = context:image('myImage')
self.sampler = ImageSampler('clamp', 'clamp', 'bilinear')
return true
end
function draw(self: DrawImage, renderer: Renderer)
if self.myImage and self.sampler then
renderer:drawImage(self.myImage, self.sampler, 'srcOver', 1)
end
end
return function(): Node<DrawImage>
return {
myImage = nil,
sampler = nil,
init = init,
draw = draw,
}
end
字段(Fields)
width
图像的宽度,在解码完成前为 0。Web 运行时会在 init 运行之后才解码图像,因此请在使用时读取,而不要只在 init 中读取一次。
self.myImage = context:image('myImage')
if self.myImage then
print("myImage width:", self.myImage.width)
end
height
图像的高度,在解码完成前为 0。Web 运行时会在 init 运行之后才解码图像,因此请在使用时读取,而不要只在 init 中读取一次。
self.myImage = context:image('myImage')
if self.myImage then
print("myImage height:", self.myImage.height)
end
方法(Methods)
view
view() -> GPUTextureView
返回一个 GPUTextureView,用于在 WGSL 着色器中采样此图像。
该视图直接包装底层 GPU 纹理——无需复制或解码。
需要 GPU 上下文(仅在文件使用 GPU 画布时可用)。
ImageAsset 的视图会在图像解码完成后采样其像素;在 Web 上这发生在 init 之后,因此请在 width 大于 0 后再获取该视图。